// 定义$的md5中会调用
var $ = {
    md5: function(val){
        return CryptoJS.MD5(val).toString();
    }
};
var navigator = {
    userAgent: "ABC"
}
var document = {
    domain: "jx.aidouer.net"
}

function TSTKC(IlI1i1) {
    var IiiIli = IlI1i1['time']
        , iillli = IlI1i1['key']
        , I1l1Ii = IlI1i1['key1']
        , I1i11i = IlI1i1['sign1']
        , I1i11l = IlI1i1['token1'];
    var llIlii = '2624';
    llIlii = llIlii.split('');
    for (var llIlil = IiiIli.split('')[llIlii[0x0]] || '\x65',
             IIlil = iillli.split('')[llIlii[0x1]] || '\x74',
             ilI1lI = I1l1Ii.split('')[llIlii[0x2]] || '\x63',
             i1ilil = I1i11i.split('')[(I1i11i.split('').length - llIlii[0x3])] || '\x6e',
             IIlii = I1i11l.split(''), IlI1il = [], IilliI = 0x0;
         (0x2 * IilliI < IIlii.length);
         IilliI++)

        switch (IlI1il.push(IIlii[ IIlii.length - IilliI - 0x1]),
        (IilliI < IIlii.length - IilliI - 0x1) && IlI1il.push(I1i11l[IilliI]),
            IilliI) {
            case 0x1:
                IlI1il.push(llIlil);
                break;
            case 0x2:
                IlI1il.push(IIlil);
                break;
            case 0x3:
                IlI1il.push(ilI1lI);
                break;
            case 0x4:
                IlI1il.push(i1ilil);
        }
    var IlI1ii = IlI1il.join('');
    return IlI1ii;
}
function encode_url(II11I1, Iii11I){
    return authCrypt.encode(II11I1, Iii11I, 0);
}
var base64 = {
    encode: function(encodedValue) {
        encodedValue = CryptoJS.enc.Utf8.parse(encodedValue);
        return CryptoJS.enc.Base64.stringify(encodedValue);
    },
    decode: function(decodeVal) {
        return CryptoJS.enc.Base64.parse(decodeVal.toString());
    }
}

function AES_CBC(i1II1, iIIl11) {
    var lIIilI = CryptoJS['enc']['Utf8']['parse'](i1II1);
    lIIilI = CryptoJS['enc']['Base64']['stringify'](lIIilI);
    var iillll = CryptoJS['enc']['Latin1']['parse'](iIIl11.substring(0x0, 0x10));
    var llIllI = CryptoJS.enc['Latin1']['parse'](iIIl11.substring(0x10, 0x20));
    var I1ilI1 = CryptoJS['AES']['encrypt'](lIIilI, iillll, {
        'iv': llIllI,
        'mode': CryptoJS.mode.CBC,
        'padding': CryptoJS.pad.Pkcs7
    });
    return I1ilI1;
}
function AES_ECB(a, b) {
    var enc = CryptoJS.AES.encrypt(a, b, {
        'mode': CryptoJS.mode.ECB,
    });
    return enc.ciphertext.toString();
    //return enc.toString();
}
function getIni(){
    var c = {
        Time: "1662471196",
        Version: Version,
        Vurl: Vurl,
        Vkey: Vkey,
        Key: Key,
        Key1: Key1,
        Key2: Key2,
        Sign: Sign,
        Sign1: Sign1,
        Sign2: Sign1,
        Token: Token,
        Token1: Token1,
        Token2: Token2
    }
    return JSON.stringify(c);
}
function lc(a){
    return lca( lcb( lcd( lco(a))) )
}
function lcf(a, b, c, d, x, s, t) {
    return lce((b & c) | ((~b) & d), a, b, x, s, t)
}
function lcg(a, b, c, d, x, s, t) {
    return lce((b & d) | (c & (~d)), a, b, x, s, t)
}
function lce(q, a, b, x, s, t) {
    return lcl(lcc(lcl(lcl(a, q), lcl(x, t)), s), b)
}
function lch(a, b, c, d, x, s, t) {
    return lce(b ^ c ^ d, a, b, x, s, t)
}
function lci(a, b, c, d, x, s, t) {
    return lce(c ^ (b | (~d)), a, b, x, s, t)
}
function lcb(x) {
    var a = 1732584193;
    var b = -271733879;
    var c = -1732584194;
    var d = 271733878;
    if (x !== "1") {
        for (var i = 0; i < x.length; i += 16) {
            var a1 = a;
            var b1 = b;
            var c1 = c;
            var d1 = d;
            a = lcf(a, b, c, d, x[i + 0], 7, -680876936);
            d = lcf(d, a, b, c, x[i + 1], 12, -389564586);
            c = lcf(c, d, a, b, x[i + 2], 17, 606105819);
            b = lcf(b, c, d, a, x[i + 3], 22, -1044525330);
            a = lcf(a, b, c, d, x[i + 4], 7, -176418897);
            d = lcf(d, a, b, c, x[i + 5], 12, 1200080426);
            c = lcf(c, d, a, b, x[i + 6], 17, -1473231341);
            b = lcf(b, c, d, a, x[i + 7], 22, -45705983);
            a = lcf(a, b, c, d, x[i + 8], 7, 1770035416);
            d = lcf(d, a, b, c, x[i + 9], 12, -1958414417);
            c = lcf(c, d, a, b, x[i + 10], 17, -42063);
            b = lcf(b, c, d, a, x[i + 11], 22, -1990404162);
            a = lcf(a, b, c, d, x[i + 12], 7, 1804603682);
            d = lcf(d, a, b, c, x[i + 13], 12, -40341101);
            c = lcf(c, d, a, b, x[i + 14], 17, -1502002290);
            b = lcf(b, c, d, a, x[i + 15], 22, 1236535329);
            a = lcg(a, b, c, d, x[i + 1], 5, -165796510);
            d = lcg(d, a, b, c, x[i + 6], 9, -1069501632);
            c = lcg(c, d, a, b, x[i + 11], 14, 643717713);
            b = lcg(b, c, d, a, x[i + 0], 20, -373897302);
            a = lcg(a, b, c, d, x[i + 5], 5, -701558691);
            d = lcg(d, a, b, c, x[i + 10], 9, 38016083);
            c = lcg(c, d, a, b, x[i + 15], 14, -660478335);
            b = lcg(b, c, d, a, x[i + 4], 20, -405537848);
            a = lcg(a, b, c, d, x[i + 9], 5, 568446438);
            d = lcg(d, a, b, c, x[i + 14], 9, -1019803690);
            c = lcg(c, d, a, b, x[i + 3], 14, -187363961);
            b = lcg(b, c, d, a, x[i + 8], 20, 1163531501);
            a = lcg(a, b, c, d, x[i + 13], 5, -1444681467);
            d = lcg(d, a, b, c, x[i + 2], 9, -51403784);
            c = lcg(c, d, a, b, x[i + 7], 14, 1735328473);
            b = lcg(b, c, d, a, x[i + 12], 20, -1926607734);
            a = lch(a, b, c, d, x[i + 5], 4, -378558);
            d = lch(d, a, b, c, x[i + 8], 11, -2022574463);
            c = lch(c, d, a, b, x[i + 11], 16, 1839030562);
            b = lch(b, c, d, a, x[i + 14], 23, -35309556);
            a = lch(a, b, c, d, x[i + 1], 4, -1530992060);
            d = lch(d, a, b, c, x[i + 4], 11, 1272893353);
            c = lch(c, d, a, b, x[i + 7], 16, -155497632);
            b = lch(b, c, d, a, x[i + 10], 23, -1094730640);
            a = lch(a, b, c, d, x[i + 13], 4, 681279174);
            d = lch(d, a, b, c, x[i + 0], 11, -358537222);
            c = lch(c, d, a, b, x[i + 3], 16, -722521979);
            b = lch(b, c, d, a, x[i + 6], 23, 76029189);
            a = lch(a, b, c, d, x[i + 9], 4, -640364487);
            d = lch(d, a, b, c, x[i + 12], 11, -421815835);
            c = lch(c, d, a, b, x[i + 15], 16, 530742520);
            b = lch(b, c, d, a, x[i + 2], 23, -995338651);
            a = lci(a, b, c, d, x[i + 0], 6, -198630844);
            d = lci(d, a, b, c, x[i + 7], 10, 1126891415);
            c = lci(c, d, a, b, x[i + 14], 15, -1416354905);
            b = lci(b, c, d, a, x[i + 5], 21, -57434055);
            a = lci(a, b, c, d, x[i + 12], 6, 1700485571);
            d = lci(d, a, b, c, x[i + 3], 10, -1894986606);
            c = lci(c, d, a, b, x[i + 10], 15, -1051523);
            b = lci(b, c, d, a, x[i + 1], 21, -2054922799);
            a = lci(a, b, c, d, x[i + 8], 6, 1873313359);
            d = lci(d, a, b, c, x[i + 15], 10, -30611744);
            c = lci(c, d, a, b, x[i + 6], 15, -1560198380);
            b = lci(b, c, d, a, x[i + 13], 21, 1309151649);
            a = lci(a, b, c, d, x[i + 4], 6, -145523070);
            d = lci(d, a, b, c, x[i + 11], 10, -1120210379);
            c = lci(c, d, a, b, x[i + 2], 15, 718787259);
            b = lci(b, c, d, a, x[i + 9], 21, -343485551);
            a = lcl(a, a1);
            b = lcl(b, b1);
            c = lcl(c, c1);
            d = lcl(d, d1)
        }
        return [a, b, c, d]
    } else {
        return [a, d, c, b]
    }
}

function lcl(x, y) {
    var a = (x & 0xFFFF) + (y & 0xFFFF);
    var b = (x >> 16) + (y >> 16) + (a >> 16);
    return (b << 16) | (a & 0xFFFF)
}
;function lcc(a, b) {
    return (a << b) | (a >>> (32 - b))
}
;function lca(t) {
    var a = "0123456789abcdef";
    var b = "";
    for (var i = 0; i < t.length * 4; i++) {
        b += a.charAt((t[i >> 2] >> ((i % 4) * 8 + 4)) & 0xF) + a.charAt((t[i >> 2] >> ((i % 4) * 8)) & 0xF)
    }
    ;return b
}
;function lco(t) {
    var a = lcb(1) + t;
    return a.replace(/[\-|\,]/g, '')
}
;function lcd(t) {
    var a = ((t.length + 8) >> 6) + 1;
    var b = new Array(a * 16);
    for (var i = 0; i < a * 16; i++)
        b[i] = 0;
    for (var i = 0; i < t.length; i++)
        b[i >> 2] |= (t.charCodeAt(i) & 0xFF) << ((i % 4) * 8);
    b[i >> 2] |= 0x80 << ((i % 4) * 8);
    b[a * 16 - 2] = t.length * 8;
    return b
}

function getInfo(clientIP){
    var sendData = {
        'url': Vurl,
        'wap': '0',
        'ios': '0',
        'host': Host,
        'referer': Ref,
        'time': Time,
        'key': Key,
        'key1': Key1,
        'sign': Sign,
        'sign1': Sign1,
        'token': Token,
        'token1': Token1,
    };

    var token0 = Vkey + '-' + Key + '-' + Sign + '-' + Token;
    var token1 = Key1 + '-' + Sign1 + '-' + Token1;
    var token2 = XXTEA.encryptToBase64(  token0 + token1 + Key2 + Sign2 + Token2, Host + Domain + Time);
    var Ckey = AES_CBC( JSON.stringify(sendData), $.md5(token2));
    var iplist = clientIP.split('.');
    var ip = iplist[0x0] + '.' + iplist[0x1] + ".000.000";
    sendData.ckey = "110#" + base64.encode(Ckey);
    var DATA = sendData;
    var token3 = TSTKC(DATA);
    var cookie = "uuid=" + Vkey + '-' + Key + '-' + Sign + '-' + Token + "; " +
        'BAIDUID=' + $.md5(token0).toUpperCase() + "; " +
        'BAIDUSID=' + $.md5(token1).toUpperCase();
    var header = {
        'Vkey': Vkey,
        'Ipmd5': $.md5(ip),
        'Version': Version,
        'Access-Token0': token0,
        'Access-Token1': token1,
        'Access-Token2': token2,
        'Access-Token3': token3,
        'cookie': cookie
    };
    var rst = {
        header: JSON.stringify(header),
        data: JSON.stringify(DATA),
        url: Api + '/Api.php' + '?ver=' + Version + '&timestamp=' + Time + '&appkey=' + $.md5(Host)
    }
    return JSON.stringify(rst);
}
function getData(result, clientIP){
    if (result.status != 1){
        return "";
    }
    var iplist = clientIP.split('.');
    var ip = iplist[0x0] + '.' + iplist[0x1] + ".000.000";
    var md5ip = $.md5(ip);
    if ($.md5(ip) != result.ipmd5){
        md5ip = $.md5(clientIP);
    }
    var info = AES_CBC_DE(result.data, $.md5(result.appkey + md5ip));
    info = JSON.parse(info);
    info.key = $.md5(Host + Token);
    //return authCrypt.decode(JSON.parse(info).url, $.md5(Host + Token));
    return JSON.stringify(info);
}

function AES_CBC_DE(encrypt, token) {
    var key = CryptoJS.enc.Latin1.parse(token.substring(0, 16));
    var iv = CryptoJS.enc.Latin1.parse(token.substring(16, 32));
    var rst = CryptoJS.AES.decrypt(encrypt, key, {
        'iv': iv,
        'mode': CryptoJS.mode.CBC,
        'padding': CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Utf8.stringify(rst);
}
